package b4.view
{
	import b4.ApplicationFacade;
	import b4.model.ResourceLoader;
	import b4.view.component.load.LoadScene;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class LoadSceneMediator extends Mediator implements IMediator
	{
		public function LoadSceneMediator(name:String, viewComponent:Object=null)
		{
			super(name, viewComponent);
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				ApplicationFacade.RESOURCE_LOADED,
				ApplicationFacade.RESOURCE_PROGRESS	
			];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var name:String = notification.getName();
			var body:Object = notification.getBody();
			var type:String = notification.getType();
			var mediatorName:String = getMediatorName(); 
			switch (name)
			{
				case ApplicationFacade.RESOURCE_LOADED:
					facade.removeMediator(getMediatorName());
				break;
				case ApplicationFacade.RESOURCE_PROGRESS:
					loadScene.loadBar.percentage = resourceLoader.vo.currentProgress;
				break;
			}
		}
		
		override public function onRegister():void
		{
			sendNotification(ApplicationMediator.ADD_CHILD, viewComponent);
		}
		
		override public function onRemove():void
		{
			loadScene.close();
		}
		
		private function get loadScene():LoadScene
		{
			return viewComponent as LoadScene;
		}
		
		private function get resourceLoader():ResourceLoader
		{
			return facade.retrieveProxy(getMediatorName()) as ResourceLoader;
		}
	}
}